package com.heima.controller;

import com.heima.pojo.Emp;
import com.heima.pojo.EmpQueryParam;
import com.heima.pojo.PageBean;
import com.heima.pojo.Result;
import com.heima.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//http://localhost:8080/emps?page=1&pageSize=5请求格式
@RequestMapping("/emps")
@RestController
public class EmpController {
    private static final Logger log = LoggerFactory.getLogger(EmpController.class);
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result list(
//            @RequestParam(defaultValue = "1") Integer page,
//            @RequestParam(defaultValue = "10") Integer pageSize,
//            //默认值是true 不传就会报错 对于非必传数据要写为@RequestParam(required = false)
//            @RequestParam(required = false) String name,
//            @RequestParam(required = false) Integer gender,
//            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
            EmpQueryParam param
    ) {
//        System.out.println(page+"=========================== "+pageSize);
        //service层处理,最后得到处理好的pageBean对象
        PageBean pageBean = empService.list(param);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result add(@RequestBody Emp emp){
        //交给service层处理
        empService.add(emp);
        return Result.success();
    }

    /**
     * 删除 根据id进行删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        empService.delete(ids);

        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Emp emp=empService.get(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result upDate(@RequestBody Emp emp){
        log.info(emp+"修改======================员工信息");
        //调用Service层
       empService.upDate(emp);

        return Result.success();
    }
}
